﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThietBiSo.Models;
using ThietBiSo.LinQDB;
using System.IO;

namespace ThietBiSo.Controllers
{
    public class logoController : Controller
    {
        //
        // GET: /Logo/

        MLogo ms = new MLogo();
        Logo s = new Logo();
        public ActionResult Index()
        {
            var v = ms.FindAll();
            ViewData["Logos"] = v;
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /SanPham/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {

                // TODO: Add insert logic here
              //  UploadFile();
                
                s.MaLogo = ms.GetMax() + 1;
                s.HienThi = true;
                s.UrlHinh = collection.Get("HinhAnh");
                s.Link = collection.Get("Link");
                if (ms.GetMaxTT() == 0 || ms.GetTotalLogo() == 0)
                {
                    s.ThuTu = 1;
                }
                else
                    s.ThuTu = ms.GetMaxTT() + 1;
                ms.Save(s);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
       

        public ActionResult Edit(int id)
        {
            s = ms.GetByID(id);
            ViewData["Logos"] = s;
            return View();
        }

        //
        // POST: /SanPham/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                
                s.MaLogo = id;
              
                if (collection.Get("HinhAnh") == null || collection.Get("HinhAnh") == "")
                {
                    s.UrlHinh = collection.Get("HiddenHinhAnh");
                }
                else
                {
                    s.UrlHinh = collection.Get("HinhAnh");
                }
                s.Link = collection.Get("Link");
                s.ThuTu = int.Parse(collection.Get("ThuTu"));
                ms.Update(s);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /SanPham/Delete/5

        public ActionResult Delete(int id)
        {
            ms.Delete(id);
            ms.SaveChange();
            return RedirectToAction("Index");
        }

        //
        // POST: /SanPham/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
